package com.company;
import org.junit.Assert;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {

    private int Num;
    private int A;

    public static ArrayList<Integer> generateAnswer(int Num){
        Random random = new Random();
        ArrayList<Integer> num = new ArrayList<Integer>();
        num.add(random.nextInt());
        if (Num!=1){
            for (int i = 0; i <= Num;){
                int a =  random.nextInt(10);
                if (num.get(i) != a){
                    num.set(i, a);
                    i++;
                }
            }
        }
        return num;
    }

    public void generateAnswerTest(int Num){
        Assert.assertEquals(NGuessNumber.generateAnswer(Num).size(),Num);
    }

    public int getPlayerInput(int Num){
        NGuessNumber nGuessNumber = new NGuessNumber();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入猜的数字！（0-9）");
        if (Num == 1){
            int anw;
            anw = scanner.nextInt();
            nGuessNumber.compareGuessAnswer(anw);
            return 1;
        }else {
            int anw = 0;
            int i = 0;
            while (i < Num){
                anw = scanner.nextInt();
                i++;
                nGuessNumber.compareGuessAnswer(anw);
            }
            return i;
        }
    }

    public void compareGuessAnswer(int Anw){
        ArrayList<Integer> sun = NGuessNumber.generateAnswer(Num);
        for (int i = 0; i < sun.size();i++){
            if (sun.get(i) != Anw){
                System.out.println("猜错了！");
                System.out.println("已获得积分：" + A + "分");
            }else {
                System.out.println("猜对了");
                A += 1;
                System.out.println("已获得积分：" + A + "分");
            }
        }
    }

    public void isWin(int Num){
        NGuessNumber nGuessNumber = new NGuessNumber();
        Scanner scanner = new Scanner(System.in);
        if (A >= Num/2){
            System.out.println("您获胜！是否继续（y/n）");
            String yes = scanner.next();
            if (Objects.equals(yes, "y")){
                nGuessNumber.kaishi();
            }
        }else if (A < Num/2){
            System.out.println("您失败！是否继续（y/n）");
            String yes = scanner.next();
            if (Objects.equals(yes, "y")){
                nGuessNumber.kaishi();
            }
        }
    }

    public void kaishi(){
        NGuessNumber nGuessNumber = new NGuessNumber();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入猜测个数！");
        Num = scanner.nextInt();
        nGuessNumber.getPlayerInputTest(Num);
        nGuessNumber.isWin(Num);
    }

    public void getPlayerInputTest(int Num){
        NGuessNumber nGuessNumber = new NGuessNumber();
        Assert.assertEquals(nGuessNumber.getPlayerInput(Num),Num);
    }

    public static void main(String[] args) {
        NGuessNumber nGuessNumber = new NGuessNumber();
        nGuessNumber.kaishi();
    }
}
